/* ================================================================
 * JSQLParser : java based sql parser 
 * ================================================================
 *
 * Project Info:  http://jsqlparser.sourceforge.net
 * Project Lead:  Leonardo Francalanci (leoonardoo@yahoo.it);
 *
 * (C) Copyright 2004, by Leonardo Francalanci
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */
 
package net.sf.sqlparser.parser;

import java.io.Reader;
import java.io.StringReader;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.JSqlParser;
import net.sf.jsqlparser.statement.Statement;

/**
 * A JSqlParser implementation that uses a parser generated by JavaCC
 */
public class SQLParserManager implements JSqlParser {
	
	/**
	 * 解析SQL语句.
	 * @param statementReader SQL语句Reader.
	 * @return 返回解析后的Statement对象.
	 */
	public Statement parse(Reader statementReader) throws JSQLParserException {
		final SQLParser parser = new SQLParser(statementReader);
		try {
			return parser.Statement();
		} catch (Throwable e) {
			throw new JSQLParserException(e);
		}
	}
	
	/**
	 * 解析SQL语句.
	 * @param statementString SQL语句.
	 * @return 返回解析后的Statement对象.
	 */
	public Statement parse(String statementString) throws JSQLParserException {
		return parse(new StringReader(statementString));
	}
	
	/**
	 * 解析SQL语句并返回解析结果对象.
	 * @param statementReader SQL语句Reader.
	 * @return 解析结果对象.
	 * @throws JSQLParserException SQL解析异常 .
	 */
	public SQLParserResult parseWithTree(Reader statementReader) throws JSQLParserException {
		final SQLParser parser = new SQLParser(statementReader);
		SQLParserResult result = null;
		try {
			result = new SQLParserResult();
			result.setStmt(parser.Statement());
			result.setStatementNode(parser.rootNode());
			return result;
		} catch (Throwable e) {
			throw new JSQLParserException(e);
		}
	}

	/**
	 * 解析SQL语句并返回解析结果对象.
	 * @param statementString SQL语句.
	 * @return 解析结果对象.
	 * @throws JSQLParserException SQL解析异常 .
	 */
	public SQLParserResult parseWithTree(String statementString) throws JSQLParserException {
		return parseWithTree(new StringReader(statementString));
	}

	/**
	 * 解析SQL的Where语句部分.
	 * @param statementReader SQL的Where语句部分Reader.
	 * @return 返回解析后的Expression对象.
	 */
	public Expression parseWhere(Reader statementReader) throws JSQLParserException {
		final SQLParser parser = new SQLParser(statementReader);
		try {
			return parser.WhereClause();
		} catch (Throwable e) {
			throw new JSQLParserException(e);
		}
	}
	
	/**
	 * 解析SQL的Where语句部分.
	 * @param statementString SQL的Where语句部分.
	 * @return 返回解析后的Expression对象.
	 */
	public Expression parseWhere(String statementString) throws JSQLParserException {
		return parseWhere(new StringReader(statementString));
	}
}
